import { HomeHttp } from '../../../api/home.js'
import utils from '../../../utils/utils'
import { servicePointMap } from '../../../utils/dataMap.js'

const homeModel = new HomeHttp()

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    servicePointId: '', // 服务点ID
    servicePointInfo: {}, // 服务点信息
    servicePointList: [], // 服务点列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
    longitude: 0, // 经度
    latitude: 0 // 维度
  },

  async initPage () {
    try {
      await this.httpWxGetLocation()
      await this.httpGetNearbyServicePoint()
    } catch {
      await this.httpGetNearbyServicePoint()
    }
  },

  onChange (event) {
    this.setData({
      servicePointId: event.detail
    });
  },

  onClick (event) {
    const { id, info } = event.currentTarget.dataset;
    this.setData({
      servicePointId: id.toString(),
      servicePointInfo: info
    });
  },

  onConfirm () {
    const { servicePointId, servicePointInfo, servicePointList } = this.data
    if (servicePointList.length === 0) {
      wx.switchTab({
        url: `/pages/home/index/index`
      })
      return
    }
    if (servicePointList.length !== 0 && !servicePointId) {
      utils.msg('请选择服务点')
      return
    }
    app.event.emit('changeServicePoint', servicePointInfo)
    // wx.setStorageSync('servicePointInfo_2', servicePointInfo)
    wx.switchTab({
      url: `/pages/home/index/index`
    })
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh () {
    this.setData({
      page: 1
    })
    this.httpGetNearbyServicePoint().then(() => {
      wx.stopPullDownRefresh()
    })
  },

  // 页面上拉触底事件的处理函数
  onReachBottom () {
    const { servicePointList, totalNum } = this.data
    if (servicePointList.length < totalNum) {
      this.setData({
        page: this.data.page + 1
      })
      this.httpGetNearbyServicePoint()
    }
  },

  // 获取附近服务点
  httpGetNearbyServicePoint () {
    const { servicePointList, longitude, latitude, page, pageSize } = this.data
    return homeModel.getNearbyServicePoint({
      page: page,
      page_size: pageSize,
      lng: longitude, // 经度
      lat: latitude // 维度
    }).then((res) => {
      let servicePointArr = servicePointMap(res.service_point || [])
      this.setData({
        servicePointList: page === 1 ? servicePointArr : servicePointList.concat(servicePointArr),
        totalNum: res.total_count
      })
    })
  },

  // 获取地理位置信息
  httpWxGetLocation () {
    let self = this
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          self.setData({
            longitude: res.longitude, // 经度
            latitude: res.latitude // 维度
          })
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
